package com.jinbo.footmessageworker.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.NavigationActivity;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.WorkerInformationBean;
import com.jinbo.footmessageworker.httpretrofit.BusinessController;
import com.jinbo.footmessageworker.httpretrofit.Listener;
import com.jinbo.footmessageworker.utils.ActivitiesHelper;
import com.jinbo.footmessageworker.utils.DialogUtils;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.view.CrossEditText;
import com.jinbo.footmessageworker.view.TitleBarView;


public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private CrossEditText et_username;
    private CrossEditText et_password;
    private Button submit;

    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }
    public static void launch(Activity activity,boolean isLoginOut) {
        Intent intent = new Intent(activity, LoginActivity.class);
        intent.putExtra("isLoginOut",isLoginOut);
        activity.startActivity(intent);
    }
    Boolean isLoginOut;//=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        isLoginOut=getIntent().getBooleanExtra("isLoginOut",false);
        initView();
        initListener();
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("登录");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        // localPreferencesHelper = new LocalPreferencesHelper(LoginActivity.this,LocalPreferencesHelper.DB_NAME);
    }

    // 初始化View
    private void initView() {
        submit = (Button) findViewById(R.id.submit);
        et_username = (CrossEditText) findViewById(R.id.username);
        et_password = (CrossEditText) findViewById(R.id.password);
    }

    // 监听
    private void initListener() {
        submit.setOnClickListener(this);
        et_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                submitEnable();
            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                submitEnable();
            }
        });
    }
    void submitEnable()
    {
        if (et_username.getText().toString().trim().length() > 0
                && et_password.getText().toString().trim().length() > 0) {
            submit.setEnabled(true);
        } else {
            submit.setEnabled(false);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.submit:
                final String username = et_username.getText().toString().trim();
                final String password = et_password.getText().toString().trim();
                BusinessController.getInstance().login(username, password, new Listener<WorkerInformationBean>() {
                    @Override
                    public void onStart(Object... params) {

                        showProgressDialog("请稍后...");
                    }

                    @Override
                    public void onComplete(WorkerInformationBean result, Object... params) {
                        dismissProgressDialog();
                        show("登录成功");// + result.user_id);
                        MyApplication.local.saveOrUpdate(LocalPreferencesHelper.USER_ID, result.user_id);//其他属性都应该为ID+""
                        String id = result.user_id;
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_SEX, result.sex);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_MOBILE, result.mobile);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_PASSWORD, password );
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_NAME, username);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_MAIl, result.mail);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_HEAD_URL, result.head);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_NICKNAME, result.nickname);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_AGE, result.age);
                        MyApplication.local.saveOrUpdate(id + LocalPreferencesHelper.USER_SPECIALITY, result.speciality);
                        NavigationActivity.launch(LoginActivity.this);
                        finish();
                        ActivitiesHelper.getInstance().closeAll();

                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                        dismissProgressDialog();
                        new DialogUtils(LoginActivity.this, "center", true, 0).setMessage(msg).setButtons(null, "确定", null).create().show();
                        //show("登录失败  "+msg);
                    }
                });


        /*        MyApplication.local.saveOrUpdate("user_name","房锦波");
               // MyApplication.local.saveOrUpdate("user_id",1);//其他属性都应该为ID+""
               // MyApplication.local.saveOrUpdate("user_sex","男");
               // MyApplication.local.saveOrUpdate("user_mail","354703398@qq.com");
                MyApplication.local.saveOrUpdate("user_phone",et_username.getText().toString());
                NavigationActivity.launch(this);*/
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        ///NavigationActivity.launch();
        if(isLoginOut)
            overridePendingTransition(0, R.anim.default_amin_out);
    }
}

